package com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.ui.chat;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.adapter.UserAdapter;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.databinding.FragmentChatBinding;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.Message;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.UserViewModel;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.GlobalViewModelStore;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.MyApplication;

import java.util.ArrayList;
import java.util.List;

public class ChatFragment extends Fragment {

    private FragmentChatBinding binding;
    private ChatViewModel mViewModel;
    private RecyclerView recyclerView;
    private UserAdapter userAdapter;
    private List<Message> messageList;
    private UserViewModel userViewModel;


    public static ChatFragment newInstance() {
        return new ChatFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentChatBinding.inflate(inflater, container, false);
        View view = binding.getRoot();

        setUserViewModel();

        setUserRecyclerView();

        return view;

    }

    private void setUserViewModel(){
        //用户身份
        // 获取全局共享的 UserViewModel
        userViewModel = GlobalViewModelStore
                .getInstance(MyApplication.getInstance())
                .get(UserViewModel.class);
    }

    private void setUserRecyclerView(){
        recyclerView = binding.userRecyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        // 添加分隔线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(dividerItemDecoration);

        messageList = new ArrayList<>();
        // 模拟用户数据
        messageList.add(new Message("avatar1", "用户名1", "时间1", "消息提示1"));
        messageList.add(new Message("avatar2", "用户名2", "时间2", "消息提示2"));
        messageList.add(new Message("avatar3", "用户名3", "时间3", "消息提示3"));

        userAdapter = new UserAdapter(requireContext(), messageList);
        recyclerView.setAdapter(userAdapter);
    }
}